﻿module TextRendering

open System.Windows.Media
open StyledElements
open ComputedStyle


let computeText text (node:IStyledText) =
    let style = node.computedStyle()
    let brush = new SolidColorBrush(style.color)
    let formatted = new FormattedText(
                        text,
                        System.Globalization.CultureInfo("en-us"),
                        System.Windows.FlowDirection.LeftToRight,
                        Typeface(
                            style.fontFamily, 
                            style.fontStyle, 
                            style.fontWeight, 
                            System.Windows.FontStretch()),
                        float(style.fontSize), (* font size given in pixels *)
                        brush)
    match style.textDecoration with
            | TextDecorationNone -> ()
            | _ ->
                let pos =
                    match style.textDecoration with
                    | TextDecorationUnderline -> System.Windows.TextDecorationLocation.Underline
                    | TextDecorationOverline -> System.Windows.TextDecorationLocation.OverLine
                    | TextDecorationLineThrough -> System.Windows.TextDecorationLocation.Strikethrough
                let decoration = new System.Windows.TextDecoration(
                        pos,
                        new Pen(brush, 1.0),
                        2.0,
                        System.Windows.TextDecorationUnit.Pixel,
                        System.Windows.TextDecorationUnit.Pixel)
                let coll = new System.Windows.TextDecorationCollection()
                coll.Add(decoration)
                formatted.SetTextDecorations(coll)
                ()
    formatted